package com.ebook.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.ebook.util.random;
public class SuperCode {
    private Connection conn = null;
    PreparedStatement statement = null;
 
    void connSQL() {
        String url = "jdbc:mysql://115.29.35.230:3306/ebook_test?useUnicode=true&characterEncoding=utf-8"; // 数据库地址，端口，数据库名称，字符集
        String username = "zhiyubook"; // 数据库用户名
        String password = "666666"; // 数据库密码
        try {
            Class.forName("com.mysql.jdbc.Driver"); // 加载驱动，必须导入包mysql-connector-java-5.1.6-bin.jar
            conn = DriverManager.getConnection(url, username, password);
        }
        // 捕获加载驱动程序异常
        catch (ClassNotFoundException cnfex) {
            System.err.println("装载 JDBC/ODBC 驱动程序失败。");
            cnfex.printStackTrace();
        }
        // 捕获连接数据库异常
        catch (SQLException sqlex) {
            System.err.println("无法连接数据库");
            sqlex.printStackTrace();
        }
    }
 
    // 关闭数据库
    void deconnSQL() {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            System.out.println("关闭数据库异常：");
            e.printStackTrace();
        }
    }
 
    /**
     * 执行查询sql语句
     *
     * @param sql
     * @return
     */
    ResultSet selectSQL(String sql) {
        ResultSet rs = null;
        try {
            statement = conn.prepareStatement(sql);
            rs = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
 
    /**
     * 执行插入sql语句
     *
     * @param sql
     * @return
     */
    boolean insertSQL(String sql) {
        try {
            statement = conn.prepareStatement(sql);
            statement.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.out.println("插入数据库时出错：");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("插入时出错：");
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 执行删除sql语句
     *
     * @param sql
     * @return
     */
    boolean deleteSQL(String sql) {
        try {
            statement = conn.prepareStatement(sql);
            statement.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.out.println("删除数据库时出错：");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("删除时出错：");
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 执行更新sql语句
     *
     * @param sql
     * @return
     */
    boolean updateSQL(String sql) {
        try {
            statement = conn.prepareStatement(sql);
            statement.executeUpdate();
            return true;
        } catch (SQLException e) {
            System.out.println("更新数据库时出错：");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("更新时出错：");
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 打印结果集
     *
     * 具体列根据自己的数据库表结构更改
     *
     * @param rs
     */
    void print(ResultSet rs) {
        System.out.println("-----------------");
        System.out.println("查询结果:");
        System.out.println("-----------------");
 
        try {
            while (rs.next()) {
                System.out.println(rs.getInt(0) + "/t/t" + rs.getString(1)
                        + "/t/t" + rs.getString(2));
            }
        } catch (SQLException e) {
            System.out.println("显示时数据库出错。");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("显示出错。");
            e.printStackTrace();
        }
    }
 
    public static void main(String args[]) {
         random  random = new random();
         SuperCode dao = new SuperCode();
        dao.connSQL(); // 连接数据库
       // String input = (random.input("3","3"));
      //  String sql = "insert into partInfo (partnerid, bookid) values (?, ?)";
        String s = "select * from users";
        System.out.println("====================================");
        System.out.println("====================================");
        System.out.println("====================================");
        System.out.print("输入 合作者id，bookid ,超级码个数 ：");
        System.out.println("输入格式为 12,99,10");
        
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
         String str1[] = null;
		try {
			str1 = in.readLine().split(",");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
           int partid = Integer.valueOf(str1[0]);
           int booid = Integer.valueOf(str1[1]);
           int count = Integer.valueOf(str1[2]);
          if(partid <0 && partid >99 )
          {
        	  System.out.println("输入 合作者id,范围错误！！！");
        	  return ;
          }else
        	  if(booid <0 && booid >999 )
              {
            	  System.out.println("输入 bookid,范围错误！！！");
            	  return ;
              }
        	  else
            	  if(booid <0 && booid >999 )
                  {
                	  System.out.println("输入超级码个数 错误！！！");
                	  return ;
                  }
       //.out.println("输入值为:"+partid+","+booid+","+count);
        String insert = "insert into partenInfo (partnerid, bookid,code) values ("+partid+","+booid+",";
  
        String update = "update users set userPWD =20000 where userID= '10000'";
        String delete = "delete from users where userID= '10000'";
        for(int i =0 ; i< count;i++)
        {    
        	//System.out.println( random.input((str1[0]), str1[1])) ;
         	String code = MD5.getEncryptedPwd(random.input((str1[0]), str1[1]));
        	insert = insert.replace("?", "\""+code+"\"");
        	//System.out.println(insert);
        	 System.out.println(code );
       	if (dao.insertSQL(insert +"\""+code+"\"" +")") == true) {
           //   System.out.println("插入成功");
      }
     
        }
        /*
        if (dao.updateSQL(update) == true) {
            System.out.println("更新成功");
            ResultSet resultSet = dao.selectSQL(s);
            dao.print(resultSet);
        }
        if (dao.insertSQL(delete) == true) {
            System.out.println("删除成功");
            ResultSet resultSet = dao.selectSQL(s);
            dao.print(resultSet);
        }
  */
      dao.deconnSQL(); // 关闭数据库连接
    
    }
}